import time
from selenium.webdriver.support.ui import WebDriverWait

#基类
class Base():

    #驱动
    def __init__(self,driver):
        self.driver=driver

    #进入网站
    def open_url(self,url):
        self.driver.get(url)

    #时间
    def time(self,name):
        t=time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot(t+name)
    #元素封装(报错截图)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.time('error.png')
            print(e)

    #点击封装
    def click(self,loc):
        self.find_element(loc).click()

    #输入封装
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)

    #进入框架
    def fname(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(loc))

    #退出框架
    def switch_to(self,loc):
        self.driver.switch_to.default_content(loc)

    #切换窗口
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])